package other;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;

// 属性绑定和监听
public class BindAndListenerDemo extends Application {
	private VBox root = new VBox(20);
	private TextField tf;
	private Text text;
	
	public static void main(String[] args) {
		launch(args);
	}
	
	@Override
	public void start(Stage stage) {
		
		root.setAlignment(Pos.CENTER);
		
		tf = new TextField();
		tf.setMaxWidth(200);
		
		text = new Text("Your Input Is:  ");
		root.getChildren().addAll(tf, text);
		
		Scene scene = new Scene(root, 300, 300);
		stage.setScene(scene);
		stage.show();
		
		// 属性绑定
//		text.textProperty().bind(tf.textProperty());        // text的值被束缚住（注意顺序）
//		text.textProperty().unbind();	// 属性解绑
		// 属性监听
//		tf.textProperty().addListener(e -> text.setText("Your Input Is:  " + tf.getText()) );
		tf.textProperty().addListener(new ChangeListener<String>() {

			@Override
			public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
				System.out.println("旧值：" + oldValue);
				System.out.println("新值：" + newValue);
			}
		});
	}
}
